package org.lc.stk.model.baseInfo;

import java.time.LocalDateTime;
import java.util.List;

import org.hibernate.annotations.Comment;
import org.lc.stk.model.course.Course;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.PrePersist;
import jakarta.persistence.PreUpdate;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;

/**
 * 专业层次
 */
@Entity
@Table(
    name = "major_cengci",
    uniqueConstraints = {
        @UniqueConstraint(columnNames = {"major_id", "cengci_id"}, name = "uk_major_cengci")
    }
)
@Comment("专业层次")
public class MajorCengci {

    public enum MajorCengciState {
        ACTIVE(0),
        DELETED(1);
        private final int value;
        MajorCengciState(int value) {
            this.value = value;
        }
        public int getValue() {
            return value;
        }
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "major_id", nullable = false)
    @Comment("专业ID")
    private Major major;
    
    @ManyToOne(optional = false)
    @JoinColumn(name = "cengci_id", nullable = false)
    @Comment("培养层次ID")
    private Cengci cengci;

    @Column(name = "order_num")
    @Comment("排序号")
    private Integer orderNum = 0;

    @Column(name = "remark")
    @Comment("备注")
    private String remark;
    
    @Column(name = "state")
    @Comment("状态：0 正常，1 已删除（软删除）")
    private Integer state = 0;

    @Column(name = "create_time")
    @Comment("创建时间")
    private LocalDateTime createTime;

    @Column(name = "update_time")
    @Comment("更新时间")
    private LocalDateTime updateTime;

    @ManyToMany
    private List<Course> courses;

    @PrePersist
    protected void onCreate() {
        createTime = LocalDateTime.now();
        updateTime = createTime;
    }

    @PreUpdate
    protected void onUpdate() {
        updateTime = LocalDateTime.now();
    }

    public List<Course> getCourses() {
        return courses;
    }

    public void setCourses(List<Course> courses) {
        this.courses = courses;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Major getMajor() {
        return major;
    }

    public void setMajor(Major major) {
        this.major = major;
    }

    public Cengci getCengci() {
        return cengci;
    }

    public void setCengci(Cengci cengci) {
        this.cengci = cengci;
    }

    public Integer getOrderNum() {
        return orderNum;
    }

    public void setOrderNum(Integer orderNum) {
        this.orderNum = orderNum;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Integer getState() {
        return state;
    }
    
    public void setState(MajorCengciState state) {
        this.state = state.getValue();
    }
    
    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof MajorCengci)) return false;
        MajorCengci that = (MajorCengci) o;
        return getId() != null && getId().equals(that.getId());
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }

    @Override
    public String toString() {
        return "MajorCengci{" +
                "id=" + id +
                ", major=" + major.getName() +
                ", cengci=" + cengci.getName() +
                ", orderNum=" + orderNum +
                '}';
    }
}
